package algo.array;

/**
 * @Title:
 * @Description:
 * @Author Fay Wang
 * @Date 2022/4/14 20:05
 * @Version 1.0
 */
public class Array {
    // 整形数组data
    private int data[];
    // 数组长度
    private int n;
    // 数组中元素实际个数
    private int count;

    // 构造方法，必须声明数量
    public Array(int capacity) {
        this.data = new int[capacity];
        this.n = capacity;
        this.count = 0;
    }

    // 查找find
    public int find(int index) {
        if (index < 0 || index >= count) {
            return -1;
        }
        return data[index];
    }

    // 插入insert
    public boolean insert(int index, int value) {
        if (count == n) {
            System.out.println("没有可插入的位置");
            return false;
        }
        // 如果count没有满
        if (index < 0 || index > count) {
            System.out.println("位置不合法");
            return false;
        }
        // 位置合法
        for (int i = count; i > index; --i) {
            data[i] = data[i - 1];
        }
        data[index] = value;
        ++count;
        return true;
    }

    // 删除delete
    public boolean delete(int index) {
        if (index < 0 || index >= count) {
            System.out.println("位置不合法");
            return false;
        }
        for (int i = index + 1; i < count; ++i) {
            data[i - 1] = data[i];
        }
        --count;
        return true;
    }

    // 打印全部
    public void printAll() {
        for (int i = 0; i < count; i++) {
            System.out.print(data[i] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Array array = new Array(5);
        array.printAll();
        array.insert(0, 3);
        array.printAll();
        array.insert(0, 4);
        array.printAll();
        array.insert(1, 5);
        array.printAll();
        array.insert(3, 9);
        array.printAll();
        array.insert(3, 10);
        array.printAll();
        array.insert(3, 11);
        array.printAll();
    }
}
